﻿using HandyControl.Controls;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AgileLogistics.View.Nocite
{
    /// <summary>
    /// Page_List.xaml 的交互逻辑
    /// </summary>
    public partial class Page_List : Page
    {
        ViewModel.NociteViewModel vm;
      
        Model.DBContext dbContext = new Model.DBContext();
        Model.Repository<Model.Notice> Rep = new Model.Repository<Model.Notice>();
        public Page_List()
        {
            InitializeComponent();
            vm = DataContext as ViewModel.NociteViewModel;
            vm.NociteList =   Rep.GetList() ;

           
        }

        private void btn_New_Click(object sender, RoutedEventArgs e)
        {
            
        }

        private void btn_Del_Click(object sender, RoutedEventArgs e)
        {
            if(vm.CurrNocite ==null)
            {
                Growl.WarningGlobal("请选择要删除的类型！");
                return;
            }

            Rep.Delete(vm.CurrNocite);
            dbContext.DB.Deleteable<Model.NoticeUsers>().Where(it => it.NoticeId == vm.CurrNocite.Id).ExecuteCommand();
           // vm.HistoryNoticeUsersList = dbContext.DB.Queryable<Model.NoticeUsers>().Where(nu => nu.UserId == vm.LoginUser.Id && nu.IsRead == true).ToList();
            Growl.SuccessGlobal("删除成功！"); 
            vm.NociteList = Rep.GetList() ;
        }

        private void SearchBar_SearchStarted(object sender, HandyControl.Data.FunctionEventArgs<string> e)
        {
            vm.NociteList =   dbContext.DB.Queryable<Model.Notice>().Where(u=>u.Title.Contains( e.Info) || u.NoticeContent.ToString()  == e.Info ).ToList() ;
        }

        private void btn_Edit_Click(object sender, RoutedEventArgs e)
        {
            if(vm.CurrNocite ==null)
            {
                Growl.WarningGlobal("请选择要撤回的通知");
                return;
            }
            vm.CurrNocite.IsRevoke = true;
            {
                dbContext.DB.Updateable<Model.Notice>().ExecuteCommand();
            }
             
           
        }
    }
}
